<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <table>
        <tr>
            <td>标题</td>
            <td><input type="text" name="title"></td>
        </tr>
        <tr>
            <td>摘要</td>
            <td><input type="text" name="digest"></td>
        </tr>
        <tr>
            <td>内容</td>
            <td><input type="text" name="content"></td>
        </tr>
        <tr>
            <td>分类</td>
            <td><input type="text" name="classify"></td>
        </tr>
        <tr>
            <td>作者</td>
            <td><input type="text" name="author"></td>
        </tr>
        <tr>
     
            <td><input type="text" value="保存" onclick="save()"></td>
            <td><input type="text" value="取消" onclick="cancel()"></td>
        </tr>
    </table>
    <script src="./js/jquery-3.6.0.min.js" ></script>
    <script src="./config/baseUrl.js" ></script>
    <script src="./js/api.js" ></script>
    <script>
        $(function(){
            let id=getUrlParam('id');
            getbyid(id).then(res=>{
                let obj=res.data;
                $('[name=title]').val(obj.title) ;
                $('[name=digest]').val(obj.digest) ;
                $('[name=content]').val(obj.content) ;
                $('[name=classify]').val(obj.classify) ;
                $('[name=author]').val(obj.author) ;
                $('[name=timedate]').val(obj.timedate) ;
            })
        })
        //获取url中的参数 其中name指的是url中的键的名称
 function getUrlParam(name) {

var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象

var r = window.location.search.substr(1).match(reg); //匹配目标参数

if (r != null) {
    return unescape(r[2]);
} else {
    return null; //返回参数值
}

}
        function save(){
            let id=getUrlParam('id');
            let obj={
                title:$('[name=title]').val(),
                digest:$('[name=digest]').val(),
                content:$('[name=content]').val(),
                classify:$('[name=classify]').val(),
                author:$('[name=author]').val(),
                timedate:$('[name=timedate]').val(),
            }
            if(id){
                updateBlog(id,obj).then(res=>{
                    location.href=`./index.html`
                })
            }else{
                let params={
                title:obj.title,
                digest:obj.digest,
                content:obj.content,
                classify:obj.classify,
                author:obj.author,
                timedate:obj.timedate,}
             addBlog(params).then(res=>{
                location.href=`./index.html`
             })
        
            }
            }
        
        function cancel(){
            location.href=`./index.html`
        }
    </script>
</body>
</html>